﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControl : MonoBehaviour
{
    public float jumpForce = 1000f;
    public float max_v = 5f;
    public float press = 100f;
    public bool FaceR { get { return faceR; } }

    public HealthBar heal;
    public Items items;
    public float hurtForce = 200f;
    public float lastHitTime;
    public float repeatHurtTime = 2f;

    public AudioClip[] ouchAudios;
    public AudioClip[] jumpAudios;
    public AudioSource heroAS = new AudioSource();

    private float h = 0;
    private bool jump = false;
    private bool faceR = true;
    private bool grounded = true;
    private Rigidbody2D self;
    private Transform groundedCheck;
    private Animator anim;

    void Start()
    {
        Rocket.playerCtrl = this;

        if (!Client.isLeft)
        {
            faceR = !faceR;
            Vector3 theScale = transform.localScale;
            transform.localScale = new Vector3(theScale.x * -1, theScale.y, theScale.z);
        }

        self = GetComponent<Rigidbody2D>();
        groundedCheck = transform.Find("GroundCheck");
        //heal = GameObject.Find("HealthBar").GetComponent<HealthBar>();
        items = GetComponent<Items>();
        anim = GetComponent<Animator>();
        lastHitTime = Time.time;

        heroAS = GetComponent<AudioSource>();
    }

    void Update()
    {
        //Debug.Log(transform.position);

        h = Input.GetAxis("Horizontal");
        grounded = Physics2D.Linecast(transform.position, groundedCheck.position, 1 << 8) 
            || Physics2D.Linecast(transform.position, groundedCheck.position, 1 << 10);
       // Debug.Log(grounded);
        if (grounded && Input.GetButtonDown("Jump")) jump = true;
    }

    private void FixedUpdate()
    {
        anim.SetFloat("speed", Mathf.Abs(h));//设置过渡条件的参数 speed = h的绝对值
        if (Mathf.Abs(self.velocity.x) > max_v)
            self.velocity = new Vector2(Mathf.Sign(self.velocity.x) * max_v, self.velocity.y);
        else
            self.AddForce(Vector2.right * h * press);
        
        if (h != 0 && (h > 0 ^ faceR)) flip();
        if(jump)
        {
            anim.SetTrigger("Jump"); //设置过渡到Jump动画的触发器
            self.AddForce(new Vector2(0, jumpForce));
            heroAS.PlayOneShot(jumpAudios[Random.Range(0, jumpAudios.Length)]);
            //heroAS.clip = jumpAudios[Random.Range(0, jumpAudios.Length)];
            //heroAS.Play();
            jump = false;
        }
    }

    void flip()
    {
        faceR = !faceR;
        Vector3 theScale = transform.localScale;
        transform.localScale = new Vector3(theScale.x * -1, theScale.y, theScale.z);

        BattleManager.SendFlip();
    }

    public bool Damage()
    {
        heroAS.PlayOneShot(ouchAudios[Random.Range(0, ouchAudios.Length)]);
        BattleManager.SendHeroDamage();
        if (heal.takeDamage() <= 0)
        {
            anim.SetTrigger("Death"); //死亡动画触发器
            Collider2D[] colliders = GetComponents<Collider2D>();
            foreach (Collider2D c in colliders) c.isTrigger = true;
            SpriteRenderer[] renderers = GetComponentsInChildren<SpriteRenderer>();
            foreach (SpriteRenderer renderer in renderers) renderer.sortingLayerName = "UI";
            GameObject.Find("Gun").GetComponent<Gun>().enabled = false;
        }

        return true;
    }

    public void OnCollisionEnter2D(Collision2D col)
    {
        if(col.gameObject.tag == "Enemy")
        {

            if(Time.time - lastHitTime > repeatHurtTime)
            {
                lastHitTime = Time.time;
             
                heroAS.PlayOneShot(ouchAudios[Random.Range(0, ouchAudios.Length)]);
                enabled = false;
                if (heal.takeDamage() <= 0)
                {
                    anim.SetTrigger("Death"); //死亡动画触发器
                    Collider2D[] colliders = GetComponents<Collider2D>();
                    foreach (Collider2D c in colliders) c.isTrigger = true;
                    SpriteRenderer[] renderers = GetComponentsInChildren<SpriteRenderer>();
                    foreach (SpriteRenderer renderer in renderers) renderer.sortingLayerName = "UI";
                    GameObject.Find("Gun").GetComponent<Gun>().enabled = false;
                }
                else
                {
                    self.AddForce(hurtForce * (transform.position - col.transform.position) + new Vector3(0, 200, 0));
                }

                enabled = true;
            }
        }
    }
}
